Imports System.IO

Public Class Script
    Inherits AbstractFileParser
  
    ' Script format:
    '
    ' Class ClassName
    ' propertyName AS datatype
    ';comment...
    ' End Class
    '
    '
    Private Function ExtractColumnName(ByVal src As String) As String
        Dim eoff As Integer = src.ToLower().IndexOf(" as ")
        If eoff < 0 Then Return ""
        Return src.Substring(0, eoff).Trim()
    End Function

    Private Function ExtractTableName(ByVal src As String) As String
        Dim out As String = src.Substring(6).Trim()
        Return out
    End Function

    Private Function ExtractDataType(ByVal src As String) As String
        Dim soff As Integer = src.ToLower().IndexOf(" as ")
        If soff < 0 Then Return ""
        Dim tmp As String = src.Substring(soff + 4).Trim().ToLower()
        If (tmp.IndexOf("datetime") > -1) Then Return "System.DateTime"
        If (tmp.IndexOf("guid") > -1) Then Return "System.Guid"
        If (tmp.IndexOf("integer") > -1) Then Return "System.Int32"
        If (tmp.IndexOf("string") > -1) Then Return "System.String"
        If (tmp.IndexOf("boolean") > -1) Then Return "System.Boolean"
        If (tmp.IndexOf("double") > -1) Then Return "System.Double"
        If (tmp.IndexOf("decimal") > -1) Then Return "System.Decimal"
        If (tmp.IndexOf("money") > -1) Then Return "System.Decimal"
        If (tmp.IndexOf("smallmoney") > -1) Then Return "System.Decimal"
        If (tmp.IndexOf("numeric") > -1) Then Return "System.Decimal"
        If (tmp.IndexOf("bigint") > -1) Then Return "System.Int64"
        If (tmp.IndexOf("smallint") > -1) Then Return "System.Int16"
        If (tmp.IndexOf("tinyint") > -1) Then Return "System.Byte"
        If (tmp.IndexOf("real") > -1) Then Return "System.Single"
        'MsgBox("Unsupported Script (.NET) data type" & vbCrLf & tmp)
        Throw New ApplicationException("Unsupported Script (.NET) data type" & vbCrLf & tmp)
    End Function

    Protected Overloads Overrides Function Parse(ByVal lines() As String, ByVal project As BBXProject) As TableList
        Dim tables As New TableList
        Dim currentTable As Table = Nothing
        Dim tmpL As String
        For i As Integer = 0 To lines.Length - 1
            tmpL = lines(i).Trim()
            If tmpL.ToLower().StartsWith("class ") Then
                currentTable = New Table
                currentTable.TableName = ExtractTableName(tmpL).Trim()
                Debug.WriteLine("create table " & i & currentTable.TableName)
            ElseIf tmpL.ToLower().StartsWith("end class") And (Not currentTable Is Nothing) Then
                tables.Add(currentTable)
                Debug.WriteLine("close table " & i & currentTable.TableName & currentTable.ColumnCount)
                currentTable = Nothing
            ElseIf (Not currentTable Is Nothing) Then
                If tmpL.ToLower().Trim().StartsWith(";") = False Then
                    Dim colObj As New Column
                    colObj.ColumnName = ExtractColumnName(tmpL).Trim()
                    colObj.DataType = ExtractDataType(tmpL)
                    Debug.WriteLine("column " & currentTable.TableName & colObj.ColumnName & " " & colObj.DataType)
                    currentTable.AddColumn(colObj)
                End If
            End If
            'add FK code here
        Next
        Return tables
    End Function
End Class
